package com.hadoopSpark.application.test;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.aliyun.configuration.ConfigurationTool;
import com.certificate.tool.SetTrustStore;
import com.common.constant.Constant;

/* 2016.9.6
 * 进行代码 优化，传入参数，变成动态的  OK
 */
public class StopApplication {

	public static void main(String[] args) {
		
		stopApplication(args[0]);

	}
	
	public static String stopApplication(String appName){
		String result = "400";
		
		// get certificate address
		try {
			SSLConnectionSocketFactory sslsf = SetTrustStore.getSSLConnection();
			// httpclient链接
			CloseableHttpClient httpClient = HttpClients.custom()
					.setSSLSocketFactory(sslsf).build();
			// args[0] 表示，传入的应用名称，就是我们自己理解的集群的名称
			HttpPost httpPost = new HttpPost(
					ConfigurationTool.getProperties(Constant.ALIYUN_PROJECTS_URL)
							+ appName + "/stop");

			// 使用客户端向服务器发送数据
			HttpResponse httpResponse = httpClient.execute(httpPost);

			if (httpResponse.getStatusLine().getStatusCode() == 200) {
//
//				System.out.println(httpResponse.getProtocolVersion() + " "
//						+ httpResponse.getStatusLine().getStatusCode() + " "
//						+ httpResponse.getStatusLine().getReasonPhrase());
				result = "200";
				System.out.println("200");
			} else {
//				System.out.println(httpResponse.getStatusLine().getStatusCode());
//				String bodyAsString = EntityUtils.toString(httpResponse.getEntity());
//				System.out.println(bodyAsString);
				System.out.println("400");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return result;
	}
}
	
	
	
	
	
